package com.hspedu.com.jdbc.practice;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author DL5O
 * @version 1.0
 * 添加记录
 */
public class Practice01 {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        Properties properties = new Properties();
        properties.load(new FileReader("src\\jdbc.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");

        //1.注册驱动
        Class.forName(driver);
        //2.得到连接
        Connection connection = DriverManager.getConnection(url,user,password);//进行注册
        for (int i = 0; i < 5; i++) {
            System.out.print("请输入管理员的名字:");
            String admin_name = sc.nextLine();
            System.out.print("请输入管理员的密码:");
            String admin_pwd = sc.nextLine();

            String sql = "insert into admin values(?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,admin_name);
            preparedStatement.setString(2,admin_pwd);
            int rows = preparedStatement.executeUpdate();

            if(rows > 0){
                System.out.println("成功");
            }else{
                System.out.println("失败");
            }
            preparedStatement.close();
            connection.close();
        }
    }
}
